<template>
  <div class="hot-recommend">
    <div class="hot-header">
      <!-- 左边 -->
      <div class="left">
        <a href="javascript:;" class="hot-a">热门推荐</a>
        <div class="tab">
          <a
            href="javascript:;"
            v-for="(item, index) in tabList"
            :key="index"
            @click="tabClick(item)"
            >{{ item.name }}</a
          >
        </div>
      </div>

      <!-- 右边 -->
      <div class="right" @click="pushToplaylist">
        <a href="javascript:;">更多</a>
        <span class="hot-right"></span>
      </div>
    </div>
    <div class="hot-body">
      <!-- 盒子 -->
      <div class="box" v-for="(item, index) in recommendSongList" :key="index">
        <div class="box-img">
          <el-image :src="item.picUrl" alt="" lazy />
          <a
            href="javascript:;"
            class="box-a"
            @click="pushToDetail(item.id)"
          ></a>
          <!-- 盒子底部 -->
          <div class="box-bottom">
            <div class="bottom-left">
              <span class="bottom-icon"></span>
              <span class="bottom-number">{{
                String(item.playCount).length > 5
                  ? `${Math.floor(item.playCount / 10000)}万`
                  : item.playCount
              }}</span>
            </div>
            <a
              href="javascript:;"
              title="播放"
              class="bottom-play"
              @click="getSongList(item.id)"
            ></a>
          </div>
        </div>
        <!-- 文字 -->
        <p class="title">
          <a href="javascript:;" @click="pushToDetail(item.id)">{{
            item.name
          }}</a>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import { getrecommendTop, getSongURL } from "@/api/api.js";
export default {
  props: {
    recommendSongList: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      tabList: [
        {
          name: "华语",
        },
        {
          name: "流行",
        },
        {
          name: "摇滚",
        },
        {
          name: "民谣",
        },
        {
          name: "电子",
        },
      ],
    };
  },
  computed: {
    // playCount() {
    //   if (item.length > 5) {
    //     let res = item.slice(0, item.length - 4)
    //     return `${res}万`
    //   }
    // }
  },
  // 得到推荐歌单歌曲id
  methods: {
    async getSongList(id) {
      let songList = [];
      const {
        playlist: { trackIds: res },
      } = await getrecommendTop(id);
      let res1 = res.slice(0, 10);
      // console.log(res1);
      // 先得到id
      for (let item of res1) {
        const { data: res2 } = await getSongURL(item.id);
        // console.log(res2);
        songList.push(res2[0].url);
      }
      // const {data: res2} = await getSongURL(res1[0].id)
      // res1.forEach(item => {
      //   songList.push(item.url)
      //  console.log(res2);
      // })

      // console.log(res1[0]);
      //  songList.push(res1.url)
      //  console.log(res1.url);

      // songList.push(res2[0].url)
      // console.log(songList);
      this.$store.commit("savaSongURL", []);
      this.$store.commit("savaSongURL", songList);
    },
    async getSongURL() {},
    // 点击跳转歌单详情
    pushToDetail(id) {
      this.$router.push({
        path: `/playlist`,
        query: {
          id,
        },
      });
    },
    // 点击跳转到歌单页
    tabClick(item) {
      this.$router.push({
        path: `/discover/playlist`,
        query: item,
      });
    },
    // 点击更多跳到歌单
    pushToplaylist() {
      this.$router.push("/discover/playlist");
    },
  },
};
</script>

<style lang="scss" scoped>
.hot-header,
.hot-right {
  background: url("https://s2.music.126.net/style/web2/img/index/index.png?6f7122023fe4b0e7c59ac1d1226d1064")
    no-repeat 0 9999px;
}

.hot-recommend {
  background: #fff;
  a {
    text-decoration: none;
  }
  .hot-header {
    height: 33px;
    padding: 0 10px 0 34px;
    background-position: -225px -156px;
    border-bottom: 2px solid #c10d0c;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      justify-content: start;
      align-items: center;
      .hot-a {
        font-size: 20px;
        color: black;
        font-weight: normal;
        margin-bottom: 5px;
      }
      .tab {
        margin-bottom: 5px;
        a {
          padding: 0 20px;
          color: #666;
          border-right: 1px solid #ccc;
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
    .right {
      margin-bottom: 5px;
      .hot-right {
        display: inline-block;
        width: 12px;
        height: 12px;
        background-position: 0 -240px;
      }
      a {
        color: #666;
      }
      &:hover {
        cursor: pointer;
      }
    }
  }
}
// 盒子部分
.bottom-icon,
.bottom-play {
  background: url("https://s2.music.126.net/style/web2/img/iconall.png?c23e7b53fa2a4c621f650c6c06e6f5c4")
    no-repeat;
}
.box-bottom {
  background: url("https://s2.music.126.net/style/web2/img/coverall.png?2ac4f0b43a8fc79457fe419254b2f516")
    no-repeat;
}
.hot-body {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .box {
    margin-top: 20px;
    .box-img {
      position: relative;
      width: 140px;
      height: 140px;
      .el-image {
        display: block;
        width: 100%;
        height: 100%;
      }
      .box-a {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: url("https://s2.music.126.net/style/web2/img/coverall.png?2ac4f0b43a8fc79457fe419254b2f516");
        background-position: 0 0;
      }
      .box-bottom {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 27px;
        background-position: 0 -537px;
        color: #ccc;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .bottom-left {
          display: flex;
          align-items: center;
          justify-content: start;
          .bottom-icon {
            display: inline-block;
            background-position: 0 -25px;
            width: 14px;
            height: 11px;
            line-height: 27px;
            margin: 0 3px;
          }
          .bottom-number {
            line-height: 27px;
          }
        }
        .bottom-play {
          line-height: 27px;
          display: inline-block;
          width: 16px;
          height: 17px;
          background-position: 0 0;
          margin-right: 10px;
          &:hover {
            background-position: 0 -60px;
          }
        }
      }
    }
    .title {
      width: 140px;
      height: 57px;
      font-size: 14px;
      margin: 8px 0 3px;
      a {
        color: #000;
        &:hover {
          text-decoration: underline;
        }
      }
    }
  }
}
</style>